// pages/mediaList/mediaList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    novelList:[],
    type:0,
    mediaType:0,
    pageNum:1,
    submitForm:{
      pageNum:1,
      pageSize:1
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let str1 = ''
    let str2 = '' 
    this.setData({
      type:options.type,
      mediaType:options.mediaType
    })
    switch(this.data.type){
      case '0':
        str1 = "热门"
        this.setData({'submitForm.views':1})
        break;
      case '1':
        str1 = "精选"
        this.setData({'submitForm.curation':1})
        break;
      case '2':
        str1 = "最新"
        this.setData({'submitForm.desc':1})
        break;
    }
    switch(this.data.mediaType){
      case '0':
        str2 = "小说"
        break;
      case '1':
        str2 = "漫画"
        break;
      case '2':
        str2 = "视频合集"
        break;
    }
    wx.setNavigationBarTitle({
      title: str1 + str2
    });
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
 /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh()
  },
  //刷新
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
  },
  //网络请求，获取数据
  getData(){
    this.setData({
      'submitForm.pageNum':this.data.pageNum,
      'submitForm.pageSize':18,
      'submitForm.mediaType':this.data.mediaType
    })
    app.$http.get('/app/collection/list', this.data.submitForm).then(res =>{
      this.setData({novelList:res.data.records})
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
    })
  },
  //滑动到底部
  reachBottom() {
    this.setData({
      'submitForm.pageNum':this.data.pageNum + 2,
      'submitForm.pageSize':9,
      'submitForm.mediaType':this.data.mediaType
    })
    app.$http.get('/app/collection/list', this.data.submitForm).then(res => {
      const records = res.data.records && res.data.records || [];
      if (records.length === 0) {
          wx.showToast({
              title: '没有更多数据了',
              icon: 'none',
              duration:500
          });
          return;
      }
      this.setData({
        pageNum: this.data.pageNum + 1,
        novelList:[...this.data.novelList,...records]
      });
    }).catch(err => {
        console.log(err);
    });
  },
  toCollection: function(e){
    const collection = e.currentTarget.dataset.item;
    wx.setStorageSync('collection', collection)
    wx.navigateTo({
      url: '/pages/index/collection/collection',
    })
  },
})